<?php

namespace app\controllers;
use yii\db\Query;
use yii\web\Controller;


class ApiEducationController extends ApiBaseController {
     //教育经历
    private $user_id;
    public function init(){
      $this->user_id=$this->GetUser()['user_id'];
    }

    //添加教育经历
    public function actionAdd(){
     $method=\Yii::$app->request->method;
     if ($method=='POST'){
      $data=\Yii::$app->request->post();
      unset($data['token']);
      $data['create_time']=time();
      $data['user_id']=$this->user_id;
      $res=\Yii::$app->db->createCommand()->insert('yii_user_education',$data)->execute();
      if ($res){
      $time['time']=time();
      \Yii::$app->db->createCommand()->update('yii_user',$time,array('user_id'=>$this->user_id))->execute();
          return json_encode(['code'=>1,'msg'=>'添加成功']);
      }else{
          return json_encode(['code'=>0,'msg'=>'添加失败']);
       }
      }
    }


    //编辑教育经历
    public function actionEdit(){
        $method=\Yii::$app->request->method;
        if ($method=='POST'){
            $data=\Yii::$app->request->post();
            unset($data['token']);
            $data['create_time']=time();
            $data['user_id']=$this->user_id;
            $where['id']=$data['id'];
            $where['user_id']=$this->user_id;
            $res=\Yii::$app->db->createCommand()->update('yii_user_education',$data,$where)->execute();
              $time['time']=time();
           \Yii::$app->db->createCommand()->update('yii_user',$time,array('user_id'=>$this->user_id))->execute();
            if ($res){
                return json_encode(['code'=>1,'msg'=>'更新成功']);
            }else{
                return json_encode(['code'=>0,'msg'=>'更新失败']);
            }
        }else{
          $where['id']=$this->GetId();
          $where['user_id']=$this->user_id;
          $list=(new Query())->select('id,school,education,major,start_time,end_time')->from('yii_user_education')->where($where)->one();
          return json_encode($list);
        }
    }


    //删除教育经历
    public function actionDel(){
       $where['id']=$this->PostId();
       $where['user_id']=$this->user_id;
       $res=\Yii::$app->db->createCommand()->delete('yii_user_education',$where)->execute();
        if ($res){
        return json_encode(['code'=>1,'msg'=>'删除成功']);
        }else{
        return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //学历
    public function actionEducation(){
    	   $list=(new Query())->from('yii_education')->where(['in','id',[2,3,4,5,6,7,8]])->all();
          return json_encode($list);
    }

}
